<?php
/**
 * 
 */
namespace liketry\components;

use yii\base\Widget;
use Yii;
use services\models\MemberScore;
use services\models\Messages;
use services\models\MemberInfo;
use common\models\LoginLog;

class TopMesages extends Widget
{

    public function run()
    {
        // 顶部导航
        $member_uid = (int) Yii::$app->session->get(\Yii::$app->params['member_no'], null);
        $username = Yii::$app->session->get(\Yii::$app->params['username'], null);
        $score = 0;
        $newmessage_count = 0;
        
        $redis = Yii::$app->redis_old;
        
        // 自动登录
        if (empty($member_uid)) {
            $redirect_url = Yii::$app->request->getHostInfo() . Yii::$app->request->url;

            if (strpos($redirect_url, 'user_login.php') !== false) {
                $redirect_url = Yii::$app->params['domains']['liketry_url'];
            }

            $cookies  = Yii::$app->request->cookies;
            
            $set_time = time()+60*60*24*30;
            
            $cookie_str = isset($_COOKIE['_utry_auto']) ?  $_COOKIE['_utry_auto'] : '';
            
            $cookie_arr = explode(':', base64_decode($cookie_str));
            
            if (! empty($cookie_str) && count($cookie_arr) == 3) {
                if ($cookie_arr[1] >= time()) {
                    $tmp_id = intval($cookie_arr[0]);
                    
                    $sql = "select salt from d2.pre_ucenter_members where uid=" . $tmp_id;
                    $query = Yii::$app->db->createCommand($sql);
                    $user = $query->queryOne();
                    $member = MemberInfo::findOne([
                        'member_no' => $tmp_id
                    ]);
                    
                    if (!empty($member) && $member->is_forbid != 'Y' && ! empty($user['salt']) && $cookie_arr[2] == md5($cookie_arr[0] . $user['salt'] . $cookie_arr[1] . '_liketry_auto_cookie5' .'')) {
                        
                        $uid = $member->member_no;
                        
                        // 当天登录过，不写login_log
                        if (substr($redis->hget("M_" . $uid, "last_login_time"), 0, 10) !== date("Y-m-d")) {
                            $gid = 0;
                            $loginLog = new LoginLog();
                            $loginLog->member_no = $uid;
                            $loginLog->addtime = time();
                            $loginLog->ip = ip2long(Yii::$app->request->getUserIP());
                            $loginLog->gid = $gid;
                        }
                        
                        $redis->hset("M_" . $uid, "last_login_time", date("Y-m-d H:i:s"));
                        
                        Yii::$app->session->set(Yii::$app->params['member_no'], $member->member_no);
                        Yii::$app->session->set(Yii::$app->params['username'], $member->username);
                        
                        $bestry_uid = $member->getMember_uid();
                        Yii::$app->session->set(Yii::$app->params['uid'], $bestry_uid);
                        
                        if (substr($member->username, 0, 5) == 'cell_') {
                            $redirect_url = Yii::$app->params['domains']['liketry_url'] . 'auth/edit_username.php';
                        }
                        echo '<script>location.href="' . $redirect_url . '";</script>';
                        exit();
                    }
                }
            }
        }
        if ($member_uid) {
            // 取试豆
            $score = MemberScore::getScore($member_uid);
            // 取消息数
            $newmessage_count = Messages::newMessagesCount($member_uid);
        }

        $data = [
            'username' => $username,
            'member_no' => $member_uid,
            'score' => $score,
            'newmessage_count' => $newmessage_count
        ];

        // return serialize($data);
        
        return $this->render('@liketry/views/layouts/top', [
            'username' => $username,
            'member_no' => $member_uid,
            'score' => $score,
            'newmessage_count' => $newmessage_count
        ]);
    }
}